import ICapability from "../interfaces/ICapability";

/** 筛选功能是否实现 */
export function filterCapability(capabilitys: ICapability[], data: any, index?: number): ICapability[] | undefined {

    const s = capabilitys
        .filter(capability => {
            if (capability.isEnable) {
                return capability.isEnable(data, index || 0);
            } else {
                return true;
            }
        }).map(capability => {
            const s = capability.subMenu ? filterCapability(capability.subMenu, data, index) : undefined
            capability.subMenu = s?.length ?? 0 > 0 ? s : undefined;
            return capability;
        });

    return s?.length ?? 0 > 0 ? s : undefined
}